package com.ynunicom.dc.dingdingcontractapp.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.ynunicom.dc.dingdingcontractapp.dto.exception.ParamNullException;
import com.ynunicom.dc.dingdingcontractapp.dto.exception.RsponseEmptyException;
import com.ynunicom.dc.dingdingcontractapp.entity.requestbodyentity.DeptRequestBody;
import com.ynunicom.dc.dingdingcontractapp.entity.requestbodyentity.GetChildDeptIdListRequestBody;
import com.ynunicom.dc.dingdingcontractapp.entity.requestbodyentity.GetDeptListRequestBody;
import com.ynunicom.dc.dingdingcontractapp.entity.requestbodyentity.TransBody;
import com.ynunicom.dc.dingdingcontractapp.service.DeptService;
import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.Map;

/**
 * @author: jinye.Bai
 * @date: 2020/5/27 16:31
 */
@Service
@Slf4j
public class DeptServiceImpl implements DeptService{

    @Resource
    RestTemplate restTemplate;

    private static final String CREATE_DEPT_URL= "https://oapi.dingtalk.com/department/create?access_token={access_token}";

    private static final String UPDATE_DEPT_URL = "https://oapi.dingtalk.com/department/update?access_token={access_token}";

    private static final String GET_CHILD_LIST_URL = "https://oapi.dingtalk.com/department/list_ids?access_token={access_token}&id={id}";

    private static final String GET_DEPT_LIST_URL = "https://oapi.dingtalk.com/department/list?access_token={access_token}&fetch_child={fetch_child}&id={id}";

    private static final String GET_DEPT_INFO_URL = "https://oapi.dingtalk.com/department/get?access_token={access_token}&id={id}";

    @SneakyThrows
    @Override
    public JSONObject addNewDept(TransBody<DeptRequestBody> transBody) {
        Map<String,String> urlVar = new HashMap<>(1);
        urlVar.put("access_token",transBody.getAccess_token());
        if ("".equals(transBody.getRequestBody().getName())||transBody.getRequestBody().getName()==null){
            throw new ParamNullException("名字不可为空");
        }
        JSONObject jsonObject = restTemplate.postForObject(CREATE_DEPT_URL,transBody.getRequestBody(),JSONObject.class,urlVar);
        if(jsonObject==null||jsonObject.isEmpty()){
            throw new RsponseEmptyException("返回数据为空");
        }
        if(!"0".equals(jsonObject.getString("errcode"))){
            log.info(String.format("部门:[%s]增加失败",transBody.getRequestBody().getName()));
        }
        else {
            log.info(String.format("部门:[%s]被增加了",transBody.getRequestBody().getName()));
        }
        return jsonObject;
    }

    @SneakyThrows
    @Override
    public JSONObject updateDept(TransBody<DeptRequestBody> transBody) {
        Map<String,String> urlVar = new HashMap<>(1);
        urlVar.put("access_token",transBody.getAccess_token());
        if ("".equals(transBody.getRequestBody().getId())||transBody.getRequestBody().getId()==null){
            throw new ParamNullException("部门Id不可为空");
        }
        JSONObject jsonObject = restTemplate.postForObject(UPDATE_DEPT_URL,transBody.getRequestBody(),JSONObject.class,urlVar);
        if(!"0".equals(jsonObject.getString("errcode"))){
            log.info(String.format("部门:[%s]更新失败",transBody.getRequestBody().getId()));
        }
        else {
            log.info(String.format("部门:[%s]被更新了",transBody.getRequestBody().getId()));
        }
        return jsonObject;
    }

    @Override
    public JSONObject getChildDeptIdList(TransBody<GetChildDeptIdListRequestBody> transBody) {
        Map<String,String> urlVar = new HashMap<>(2);
        urlVar.put("access_token",transBody.getAccess_token());
        urlVar.put("id",transBody.getRequestBody().getId());
        return restTemplate.getForObject(GET_CHILD_LIST_URL,JSONObject.class,urlVar);
    }

    @Override
    public JSONObject list(TransBody<GetDeptListRequestBody> transBody) {
        Map<String,String> urlVar = new HashMap<>(3);
        urlVar.put("access_token",transBody.getAccess_token());
        urlVar.put("id",transBody.getRequestBody().getId());
        urlVar.put("fetch_child",transBody.getRequestBody().getFetch_child());
        return restTemplate.getForObject(GET_DEPT_LIST_URL,JSONObject.class,urlVar);
    }

    @Override
    public JSONObject info(TransBody<String> transBody) {
        Map<String,String> urlVar = new HashMap<>(2);
        urlVar.put("access_token",transBody.getAccess_token());
        urlVar.put("id",transBody.getRequestBody());
        return restTemplate.getForObject(GET_DEPT_INFO_URL,JSONObject.class,urlVar);
    }


}
